package service

import (
	ClickHouse "gin/database"
	"gin/model"
	"gin/types"
	"github.com/gin-gonic/gin"
)

type DirectorStar struct {
}

// List 关注列表
func (D DirectorStar) List(ctx *gin.Context, Req types.DirectorStarReq, page []int) (resList types.DirectorStarRes, err error) {

	ModuleModule := model.Module{}
	module, _ := ModuleModule.GetByCode(Req.ModuleCode)

	var entityCount struct {
		RoleCount                int64 `json:"roleCount"`                // 角色数量
		PropsCount               int64 `json:"propsCount"`               // 道具数量
		LevelCount               int64 `json:"levelCount"`               // 关卡数量
		SolveShotsCount          int64 `json:"solveShotsCount"`          // 解算镜头数量
		SpecialEffectsShotsCount int64 `json:"SpecialEffectsShotsCount"` // 特效镜头数量
		TotalCount               int64 `json:"totalCount"`               // 总数
	}

	query := ClickHouse.DB.Model(&model.Entity{}).
		Select("count(simpleJSONExtractString(entity.json, 'asset_category') = '角色' or null) as RoleCount,"+
			"count(simpleJSONExtractString(entity.json, 'asset_category') = '道具' or null) as PropsCount,"+
			"count(simpleJSONExtractString(entity.json, 'asset_category') = '关卡' or null) as LevelCount,"+
			"count((simpleJSONExtractString(entity.json, 'shot_cloth_solution') <> '不需要解算') or null) as solveShotsCount,"+
			"count((simpleJSONExtractString(entity.json, 'shot_fx') <> '无特效') or null) as SpecialEffectsShotsCount,"+
			"count(entity.id) as TotalCount").
		Where("entity.module_id = ? AND entity.project_id = ?", module.Id, Req.ProjectId)
	if Req.StartType == "asset" {
		query = query.Where("entity.director_star = 'yes'")
	}
	if Req.StartType == "shot" {
		query = query.Where("entity.key_shot = 'yes'")
	}
	err = query.Find(&entityCount).Error

	var entityList []types.DirectorStarList
	queryList := ClickHouse.DB.Model(&model.Entity{}).
		Select("simpleJSONExtractString(entity.json, 'asset_category') as Category,"+
			"simpleJSONExtractString(entity.json, 'asset_category') as Category,"+
			"entity.name as EntityName,"+
			"entity.id as Id,"+
			"entity.initial_episode_code as InitialEpisodeCode,"+
			"entity.showings_number,"+
			"simpleJSONExtractString(entity.json, 'asset_grade') as Grade,"+
			"simpleJSONExtractString(entity.json, 'shot_cloth_solution') as ShotClothSolution,"+
			"simpleJSONExtractString(entity.json, 'shot_fx') as ShotFx").
		Where("entity.module_id = ? AND entity.project_id = ?", module.Id, Req.ProjectId)
	if Req.StartType == "asset" {
		queryList = queryList.Where("entity.director_star = 'yes'")
	}
	if Req.StartType == "shot" {
		queryList = queryList.Where("entity.key_shot = 'yes'")
	}
	err = queryList.Offset((page[0] - 1) * page[1]).Limit(page[1]).Find(&entityList).Error

	// 导演关注类型-资产
	if Req.StartType == "asset" {

		resList.CountList = append(resList.CountList, types.DirectorStarCountList{
			Name: "角色",
			Num:  entityCount.RoleCount,
		})
		resList.CountList = append(resList.CountList, types.DirectorStarCountList{
			Name: "道具",
			Num:  entityCount.PropsCount,
		})
		resList.CountList = append(resList.CountList, types.DirectorStarCountList{
			Name: "关卡",
			Num:  entityCount.LevelCount,
		})
		for _, v := range entityList {
			resList.List = append(resList.List, types.DirectorStarList{
				Id:                 v.Id,
				Category:           v.Category,
				Grade:              v.Grade,
				InitialEpisodeCode: v.InitialEpisodeCode,
				ShowingsNumber:     v.ShowingsNumber,
				EntityName:         v.EntityName,
			})
		}
	}

	// 导演关注类型-镜头
	if Req.StartType == "shot" {

		resList.CountList = append(resList.CountList, types.DirectorStarCountList{
			Name: "解算镜头",
			Num:  entityCount.SolveShotsCount,
		})
		resList.CountList = append(resList.CountList, types.DirectorStarCountList{
			Name: "灯光镜头",
			Num:  entityCount.TotalCount,
		})
		resList.CountList = append(resList.CountList, types.DirectorStarCountList{
			Name: "特效镜头",
			Num:  entityCount.SpecialEffectsShotsCount,
		})
		for _, v := range entityList {
			resList.List = append(resList.List, types.DirectorStarList{
				Id:                 v.Id,
				Category:           v.Category,
				Grade:              v.Grade,
				InitialEpisodeCode: v.InitialEpisodeCode,
				ShowingsNumber:     v.ShowingsNumber,
				EntityName:         v.EntityName,
				ShotFx:             v.ShotFx,
				ShotClothSolution:  v.ShotClothSolution,
			})
		}
	}

	return
}
